package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.query.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void delete(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Customer customer) {
        //获取当前登录用户、
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        //设置录入人
        customer.setInputuser(employee);
        //设置销售人员
        customer.setSeller(employee);
        //设置录入时间
        customer.setInputTime(new Date());

        customerMapper.insert(customer);
    }

    @Override
    public Customer get(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> listAll() {
        return customerMapper.selectAll();
    }

    @Override
    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

    /**
     *
     * @param qo
     * @return
     */
    @Override
    public PageInfo query(CustomerQuery qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"input_time desc");//排序规则：时间安排降序

        //查询结果集
        List<Customer> list = customerMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Employee> selectByRoleSn(String... sns) {
        return customerMapper.selectByRoleSn(sns);
    }

    @Override
    public Customer selectByTel(String tel) {
        Customer customer = customerMapper.selectByTel(tel);
        return customer;
    }

    @Override
    public String getTelById(Long id) {
        return customerMapper.getTelById(id);
    }

    @Override
    public String getQQById(Long id) {
        return customerMapper.getQQById(id);
    }

    @Override
    public Customer selectByQQ(String qq) {
        Customer customer = customerMapper.selectByQQ(qq);
        return customer;
    }

    @Override
    public Integer[] selectByStatus() {
        return customerMapper.selectByStatus();
    }
}
